import request from "../../../untils/request";
import PubSub from 'pubsub-js';

Page({
    data: {
        day: '',
        month: '',
        recommendList: '',
        index: 0
    },
    onLoad: function (options) {
        // 判断是否登录
        let userInfo = wx.getStorageSync('userInfo')
        if (!userInfo) {
            wx.showToast({
                title: '请先登录',
                icon: 'none',
                success: () => {
                    wx.reLaunch({
                        url: '/pages/login/login'
                    })
                }
            })
        }

        // 更新日期
        this.setData({
            day: new Date().getDate(),
            month: new Date().getMonth() + 1
        })
        this.getRecommendList()
        PubSub.subscribe('switchType', (msg, type) => {
            let {recommendList, index} = this.data;
            if (type === 'pre') {
                (index === 0) && (index = recommendList.length)
                index -= 1;
            } else {
                (index === recommendList.length - 1) && (index = -1)
                index += 1;
            }
            this.setData({
                index
            })
            let musicId = recommendList[index].id;
            PubSub.publish('musicId', musicId)
        })
    },

    //获取每日数据
    async getRecommendList() {
        let recommendListData = await request('/recommend/songs');
        this.setData({
            recommendList: recommendListData.recommend
        })
    },
    // 跳到歌曲页面
    toSongDetail(event) {
        let {song, index} = event.currentTarget.dataset;
        this.setData({
            index
        })

        // 路由跳转传参
        wx.navigateTo({
            url: '/songPackage/pages/songDetail/songDetail?musicId' + song.id
        })
    }


});
